package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;
import com.hctforgreen.greenservice.utils.AsyncImageLoader;
import com.hctforgreen.greenservice.utils.AsyncImageLoader.ImageCallback;
import com.hctforgreen.greenservice.utils.Md5Util;

/**
 * 视频下载适配器
 * 
 * @author dhb 20141029
 * 
 */
public class VideoHistoryAdapter extends BaseAdapter {

	private Context context;
	private List<VideoPartEntity> myList;
	private ListView listView;

	public VideoHistoryAdapter(Context context, List<VideoPartEntity> myList,
			ListView listView) {
		this.context = context;
		this.myList = myList;
		this.listView = listView;
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_video_history_layout, null);
			holder.img = (ImageView) convertView.findViewById(R.id.img_video);
			holder.part = (TextView) convertView
					.findViewById(R.id.tv_video_part);
			holder.name = (TextView) convertView
					.findViewById(R.id.tv_video_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final VideoPartEntity partEntity = myList.get(position);

		holder.name.setText(partEntity.videoName + "_" + partEntity.part);
		if (partEntity.picUrl != null && partEntity.picUrl.trim().length() > 0) {
			holder.img.setTag(Md5Util.md5(partEntity.picUrl));
			AsyncImageLoader imageLoader = new AsyncImageLoader();
			Drawable bitmap = imageLoader.loadDrawable(partEntity.picUrl,
					context, true, new ImageCallback() {

						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							ImageView imageView = (ImageView) listView
									.findViewWithTag(Md5Util
											.md5(partEntity.picUrl));
							if (imageView != null && imageDrawable != null) {
								imageView.setImageDrawable(imageDrawable);
							}
						}
					});
			if (bitmap != null) {
				holder.img.setImageDrawable(bitmap);
			}

		} else {
			holder.img.setImageResource(R.drawable.ic_default_pic);
		}

		holder.part.setText(context.getString(R.string.video_history_parts_tag)
				+ partEntity.part
				+ context.getString(R.string.video_update_part_footer));
		holder.part.setVisibility(View.GONE);
		return convertView;
	}

	class ViewHolder {
		ImageView img;//
		TextView name;// 名称
		TextView part;// 剧集

	}

}